﻿
namespace Chess.Domain
{
	public class Game
	{
		/// <summary>
		/// 
		/// </summary>
		public Game()
		{
			_White = new Player(Color.White);
			_Black = new Player(Color.Black);

			// These can be set to Mock objects via internal properties
			// for unit testing.
			_DetermineGame = new GameDeterminant();
			_DetermineMove = new ValidMoveDeterminant();
		}


		#region " Properties "

		private bool _WhiteToMove = true;

		/// <summary>
		/// 
		/// </summary>
		public bool WhiteToMove
		{
			get
			{
				return _WhiteToMove;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool BlackToMove
		{
			get
			{
				return !_WhiteToMove;
			}
		}

		private Player _Black;

		/// <summary>
		/// 
		/// </summary>
		public Player Black
		{
			get { return _Black; }
		}

		private Player _White;

		/// <summary>
		/// 
		/// </summary>
		public Player White
		{
			get { return _White; }
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsCheckmate
		{
			get
			{
				return DetermineGame.IsCheckmate(this);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsStalemate
		{
			get
			{
				return DetermineGame.IsStalemate(this);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsDraw
		{
			get
			{
				return DetermineGame.IsDraw(this);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsOver
		{
			get
			{
				return this.IsCheckmate
					|| this.IsStalemate
					|| this.IsDraw;
			}
		}

		private IGameDeterminant _DetermineGame;

		/// <summary>
		/// 
		/// </summary>
		internal IGameDeterminant DetermineGame
		{
			get { return _DetermineGame; }
			set { _DetermineGame = value; }
		}

		private IValidMoveDeterminant _DetermineMove;

		/// <summary>
		/// 
		/// </summary>
		internal IValidMoveDeterminant DetermineMove
		{
			get { return _DetermineMove; }
			set { _DetermineMove = value; }
		}

#endregion


		/// <summary>
		/// 
		/// </summary>
		public void Start()
		{
			
		}

		/// <summary>
		/// 
		/// </summary>
		public void Load()
		{

		}

        /// <summary>
        /// 
        /// </summary>
        public void Move()
        {
            Piece piece = new Bishop();
            Position position = new Position();

            if (DetermineMove.IsValid(this, piece, position))
            {

                ToggleOtherPlayersTurnToMove();
            }
        }


		#region " Helper Methods "

		/// <summary>
		/// 
		/// </summary>
		private void ToggleOtherPlayersTurnToMove()
		{
			if (_WhiteToMove)
			{
				_WhiteToMove = false;
			}
			else
			{
				_WhiteToMove = true;
			}
		}

		#endregion
	}
}
